package com.tongshu.multiplefiles.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 分块上传工具类
 */
public class UploadUtils {
    /**
     * 内部类记录分块上传文件信息
     */
    private static class Value {
        String[] chunkName;
        boolean[] status;
        String[] chunkMd5;
        boolean fileStatus;

        Value(int n) {
            this.chunkName = new String[n + 1];
            this.status = new boolean[n + 1];
            this.status[0] = true;
            this.chunkMd5 = new String[n + 1];
            this.fileStatus = false;
        }
    }

    private static Map<String, Value> chunkMap = new HashMap<>();

    /**
     * 判断文件所有分块是否已上传
     *
     * @param key
     * @return
     */
    public static boolean isUploaded(String key) {
        if (isExist(key)) {
            for (boolean b : chunkMap.get(key).status) {
                if (!b)
                    return false;
            }
            return true;
        }
        return false;
    }

    /**
     * 判断文件是否有分块已上传
     *
     * @param key
     * @return
     */
    public static boolean isExist(String key) {
        return chunkMap.containsKey(key);
    }

    public static boolean chunkIsUploaded(String key, int chunk) {
        return chunkMap.get(key).status[chunk];
    }


    /**
     * 当前分块上传成功
     *
     * @param key
     * @param chunk
     */
    public static void chunkSuccess(String key, int chunk) {
        chunkMap.get(key).status[chunk] = true;
    }

    /**
     * 从map中删除键为key的键值对
     *
     * @param key
     */
    public static void removeKey(String key) {
        if (isExist(key)) {
            chunkMap.remove(key);
        }
    }

    /**
     * 获取随机生成的文件名
     *
     * @param key
     * @param chunks
     * @return
     */
    public static String getFileName(String key, String chunkMd5, int chunks, int chunk) {
        if (!isExist(key)) {
            synchronized (UploadUtils.class) {
                if (!isExist(key)) {
                    chunkMap.put(key, new Value(chunks));
                    chunkMap.get(key).chunkName[chunk] = key + "_" + chunk;
                    chunkMap.get(key).chunkMd5[chunk] = chunkMd5;
                }
            }
        }
        synchronized (UploadUtils.class) {
            if (isExist(key)) {
                if (chunkMap.get(key).chunkName[chunk] == null)
                    chunkMap.get(key).chunkName[chunk] = key + "_" + chunk;
            }
        }
        return chunkMap.get(key).chunkName[chunk];
    }

    public static String getExistFileName(String key, int chunk) {
        return chunkMap.get(key).chunkName[chunk];
    }

    public static boolean mergeChunks(String target, Long targetSize, Integer chunks, String key) throws IOException {
        // 合并所有分块
        if (!chunkMap.get(key).fileStatus) {
            for (int i = 0; i < chunks; i++) {
                String source = chunkMap.get(key).chunkName[i + 1];
                FileUtils.writeWithBlock1(target, targetSize, source, chunks, i + 1);
            }
            chunkMap.get(key).fileStatus = true;
            return true;
        }
        return false;
    }

    // 获取当前未上传的文件分块索引
    public static List<Integer> disUploaded(String key) {
        List<Integer> unUploaded = new ArrayList<>();
        for (int i = 1; i < chunkMap.get(key).status.length; i++) {
            if (!chunkMap.get(key).status[i])
                unUploaded.add(i);
        }
        return unUploaded;
    }

    // 返回当前文件上传状态，未上传，已上传，上传部分

}
